package com.rzico.sbl.ui.report

import android.widget.ImageView
import androidx.constraintlayout.widget.ConstraintLayout
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.CommonData
import com.rzico.sbl.viewmodel.ReportViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common.helper.intValue
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeBySuccess
import com.xinnuo.slimadapter.SlimAdapter
import org.json.JSONObject

class ReportCouponActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    override fun getViewModel() = get<ReportViewModel>()

    override fun initData() {
        initTitle("卡券")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData()
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关优惠信息！"
            }

            recycleList.also { layout ->
                layout.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData() }
                )

                layout.addItemDecoration(SpaceTBDecoration())

                mAdapter = SlimAdapter.creator()
                    .register<CommonData>(R.layout.item_report_coupon) { jector, bean, _ ->
                        jector.apply {
                            text(
                                R.id.item_coupon_price,
                                when (bean.id) {
                                    "4" -> "余额: ${bean.amount} 元"
                                    "3" -> "剩余: ${bean.amount} 只"
                                    else -> "剩余: ${bean.name} 张 合计: ${bean.amount} 元"
                                }
                            )

                            text(
                                R.id.item_coupon_info,
                                when (bean.id) {
                                    "3" -> "说明：此预存空桶仅限安迪系品牌空桶抵扣，仅限水站下单时使用。"
                                    "4" -> "说明：此券长期有效不找零、不兑现，仅限水站下单抵扣运费使用。"
                                    else -> "说明：此券长期有效不找零、不兑现，仅限水站使用，除天龙泉均可使用。"
                                }
                            )

                            with<ConstraintLayout>(R.id.item_coupon) {
                                when (bean.id) {
                                    "3" -> it.setBackgroundResource(R.mipmap.icon_report_v2_05)
                                    "4" -> it.setBackgroundResource(R.mipmap.icon_report_v2_07)
                                    else -> it.setBackgroundResource(R.mipmap.icon_report06)
                                }
                            }

                            with<ImageView>(R.id.item_coupon_img) {
                                when (bean.id) {
                                    "0" -> it.setImageResource(R.mipmap.icon_report07)
                                    "1" -> it.setImageResource(R.mipmap.icon_report08)
                                    "2" -> it.setImageResource(R.mipmap.icon_report09)
                                    "3" -> it.setImageResource(R.mipmap.icon_report_v2_04)
                                    "4" -> it.setImageResource(R.mipmap.icon_report_v2_06)
                                }
                            }

                            clicked(R.id.item_coupon_look) {
                                startActivityEx<ReportCouponRecordActivity>(
                                    "index" to bean.id
                                )
                            }
                        }
                    }
                    .attachTo(layout)
            }
        }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .reportCpn()
            .bindLifeCycle(getLifecycleOwner())
            .subscribeBySuccess(
                event = getViewModel(),
                onSuccess = {
                    val obj = JSONObject(it)

                    mList.apply {
                        clear()

                        // 运费余额
                        add(CommonData().apply {
                            name = obj.optNotEmpty("adAdvanceStock", "0")
                            amount = name
                            id = "3"
                        })
                        // 预存空桶
                        add(CommonData().apply {
                            name = obj.optNotEmpty("freightAmount", "0")
                            amount = name.formatDecimal()
                            id = "4"
                        })
                        // 代金券一
                        add(CommonData().apply {
                            name = obj.optNotEmpty("coupon", "0")
                            amount = (name.intValue * 0.3).formatDecimal()
                            id = "0"
                        })
                        // 代金券二
                        add(CommonData().apply {
                            name = obj.optNotEmpty("coupon2", "0")
                            amount = (name.intValue * 0.3).formatDecimal()
                            id = "1"
                        })
                        // 代金券三
                        add(CommonData().apply {
                            name = obj.optNotEmpty("coupon3", "0")
                            amount = (name.intValue * 2.0).formatDecimal()
                            id = "2"
                        })
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding
                        .listLayout
                        .swipeRefresh
                        .isRefreshing = false
                }
            )
    }

}